package priv.tuyou.system.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import priv.tuyou.bo.AppData;
import priv.tuyou.bo.Page;
import priv.tuyou.constants.WebContext;
import priv.tuyou.dao.IDao;
import priv.tuyou.exception.ServiceException;
import priv.tuyou.system.dao.ISystemUserDao;
import priv.tuyou.system.po.SystemUser;
import priv.tuyou.system.service.ISystemUserService;
import priv.tuyou.util.ResourceUtil;

@Controller
public class SystemUserController {
	
	@Resource ISystemUserService service;
	
	@Resource IDao dao;
	@Resource ISystemUserDao systemUserDao;
	
	/**
	 * 进入系统首页
	 * @author：涂有
	 * @date 2016年12月25日 上午12:44:07
	 * @return
	 * @throws
	 */
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public String index(){
		
		return "WEB-INF/jsp/manager/common/index";
	}
	
	@RequestMapping(value="/login", method=RequestMethod.POST)
	@ResponseBody
	public AppData login(String username, String password) throws ServiceException{
		
		SystemUser user = service.login(username, password);
		return new AppData(1, ResourceUtil.getText("login.success"), user);
	}
	
	/**
	 * 系统注销
	 * @date 2016年12月20日 下午11:26:11
	 * @return
	 * @throws
	 */
	@RequestMapping(value="/logout", method=RequestMethod.GET)
	public String logoutPage(HttpSession session){
		
		//销毁session，并跳转到登录页面
		session.invalidate();
		return "redirect:/index.action";
	}
	
	/**
	 * 系统注销
	 * @date 2016年12月20日 下午11:26:11
	 * @return
	 * @throws
	 */
	@RequestMapping(value="/logout", method=RequestMethod.POST)
	@ResponseBody
	public AppData logout(HttpSession session){
		
		//销毁session，并跳转到登录页面
		session.invalidate();
		return new AppData(1, "注销成功", null);
	}
	
	/**
	 * 系统用户管理
	 * @return
	 */
	@RequestMapping(value="/manage", method=RequestMethod.GET)
	public ModelAndView manage(){
		
		ModelAndView mv = new ModelAndView("WEB-INF/jsp/manager/systemUser/manage");
		Page<SystemUser> page = WebContext.getPage(SystemUser.class);
		dao.pageQuery(SystemUser.class, page);
		return mv;
	}
	
	@RequestMapping(value="/resetmypasswd", method=RequestMethod.POST)
	@ResponseBody
	public AppData resetMyPasswd(String oldPassword, String password) throws ServiceException{
		
		SystemUser user = WebContext.getSystemUser();
		if(!DigestUtils.md5Hex(oldPassword).equalsIgnoreCase(user.getPassword())){
			
			return new AppData(0, "输入密码不正确");
		}
		
		service.update(user.getId(), password);
		return new AppData(1, "重置密码成功");
	}
	
	/**
	 * 删除系统用户
	 * @author：涂有
	 * @date 2016年12月25日 上午2:01:18
	 * @param pageName
	 * @param id
	 * @return
	 * @throws
	 */
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	@ResponseBody
	public AppData delete(Long id){
		
		dao.delete(SystemUser.class, id);
		return new AppData(1, "删除成功");
	}
	
	/**
	 * 进入系统用户新增、编辑、更新密码页面
	 * {"/add", "/edit", "/resetPasswd"}
	 * @return
	 */
	@RequestMapping(value="/{pageName}", method=RequestMethod.GET)
	public ModelAndView addPage(@PathVariable("pageName") String pageName, Long id){
		
		ModelAndView mv = new ModelAndView();
		if(id != null){
			
			mv.addObject("user", dao.get(SystemUser.class, id));
		}
		
		if("add".equalsIgnoreCase(pageName)){
			
			mv.setViewName("WEB-INF/jsp/manager/systemUser/add");
		}else if("edit".equalsIgnoreCase(pageName)){
			
			mv.setViewName("WEB-INF/jsp/manager/systemUser/edit");
		}else if("resetPasswd".equalsIgnoreCase(pageName)){
			
			mv.setViewName("WEB-INF/jsp/manager/systemUser/resetPasswd");
		}
		return mv;
	}
	
	/**
	 * 系统用户新增、编辑、更新密码
	 * "/add", "/edit", "/resetPasswd"
	 * @return
	 * @throws ServiceException 
	 */
	@RequestMapping(value="/{operType}", method=RequestMethod.POST)
	@ResponseBody
	public AppData add(@PathVariable("operType") String operType, @Valid SystemUser user, BindingResult result) throws ServiceException{
		
		String msg = null;
		if("add".equalsIgnoreCase(operType)){
			
			service.add(user.getUsername(), user.getPassword(), user.getNickname(), user.getPhone());
			msg = "新增用户成功";
		}else if("edit".equalsIgnoreCase(operType)){
			
			service.update(user.getId(), user.getUsername(), user.getNickname(), user.getPhone());
			msg = "更新用户资料成功";
		}else if("resetPasswd".equalsIgnoreCase(operType)){
			
			service.update(user.getId(), user.getPassword());
			msg = "重置密码成功";
		}
		return new AppData(1, msg);
	}
}
